﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Utilities;

namespace ManageTicket.Config
{
    public partial class DataSetup : frmBase
    {
        #region Contructor
        public DataSetup()
        {
            InitializeComponent();
        }
        #endregion

        #region Variable
        ManageTicket.Library.Control.ExecuteControl exeCtrl;

        //permission
        bool bAdd = false;
        bool bEdit = false;
        bool bDelete = false;
        bool bCancel = false;
        bool bSave = false;
        bool bFunction = false;
        bool bView = false;
        bool bFind = false;
        bool bExport = false;
        bool bSendmail = false;
        bool bPrint = false;

        clsMethod4All objMethod = new clsMethod4All();
        ManageTicket.Library.Common.CommonFunction clsCommonFunc = new ManageTicket.Library.Common.CommonFunction();
        private Business.CodeType boCodeType = new Business.CodeType();
        string sGroup = "";
        string sModel = ""; //Store AND or UPDATE status
        private const string ADD_MODEL = "ADD";
        private const string UPDATE_MODEL = "UPDATE";
        #endregion

        #region Event
        private void DataSetup_Load(object sender, EventArgs e)
        {
            //execute control
            exeCtrl = new ManageTicket.Library.Control.ExecuteControl();
            exeCtrl.Dock = DockStyle.Fill;
            exeCtrl.lblDescription.Text = "Cập nhật danh mục";
            pnlExe.Controls.Add(exeCtrl);

            //permission
            clsCommonFunc.GetPermission(ref bAdd, ref bEdit, ref bDelete, ref bCancel, ref bSave, ref bFunction, ref bView, ref bFind, ref bExport, ref bSendmail, ref bPrint, frmMain.CurrentUser.UserId, "FORM_19");

            BuildTree();

            LoadStatusOnce();
            LoadStatus(false, false, false, false, false);
        }

        private void treeView1_BeforeSelect(object sender, TreeViewCancelEventArgs e)
        {
            sModel = "";
            sGroup = "";
            txtId.Text = "";
            txtName.Text = "";
            lvwItem.Items.Clear();
        }
        
        private void treeView1_DoubleClick(object sender, EventArgs e)
        {
            string s = treeView1.SelectedNode.Text;
            switch (s)
            {
                case "Giới tính":
                    sGroup = "GENDER";
                    break;
                case "Quốc tịch":
                    sGroup = "NATION";
                    break;
                case "Học vấn":
                    sGroup = "EDU";
                    break;
                case "Ngân hàng":
                    sGroup = "BANK";
                    break;
                case "Loại khách hàng":
                    sGroup = "KOC";
                    break;
                //case "Dịch vụ":
                //    sGroup = "SERV";
                //    break;
                case "Hãng vận chuyển":
                    sGroup = "TRANS";
                    break;
            }

            string name = s.ToLower();
            grpDetail.Text = "Cập nhật " + name;
            lblId.Text = "Mã " + name;
            lblName.Text = "Tên " + name;
            colId.Text = "Mã " + name;
            colName.Text = "Tên " + name;

            LoadData();
            IsEdit();
        }

        private void lvwItem_SelectedIndexChanged(object sender, EventArgs e)
        {
            sModel = "";
            IsEdit();
            if (lvwItem.SelectedItems.Count > 0)
            {
                ListViewItem item = lvwItem.SelectedItems[0];
                txtId.Text = item.Text;
                txtName.Text = item.SubItems[1].Text;

                LoadStatus(true, true, true, false, false);
            }
            else
            {
                LoadStatus(true, false, false, false, false);
            }
        }
        #endregion

        #region Execute
        public override void Add()
        {
            txtId.Text = sGroup + "_" + (boCodeType.GetMaxID(sGroup) + 1).ToString();
            txtName.Text = "";

            LoadStatus(false, false, false, true, true);
            sModel = ADD_MODEL;
            IsEdit();
            txtName.Focus();
        }

        public override void Edit()
        {
            if (sGroup == "")
                return;
            LoadStatus(false, false, false, true, true);
            sModel = UPDATE_MODEL;
            IsEdit();
            txtName.Focus();
        }

        public override void Delete()
        {
            try
            {
                if (txtId.Text == "")
                    return;

                if (MessageBox.Show("Bạn có thực sự muốn xóa?", "Aquarius", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    //Check Existing in table
                    //if (this.CheckExitToDelete(oDept.DEPT_CODE)) {
                    //    MessageBox.Show(".", "Aquarius", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    //    return;
                    //}

                    //delete
                    boCodeType.DeleteByPK(txtId.Text);

                    MessageBox.Show("Xóa thành công.", "Aquarius", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    LoadData();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Aquarius", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public override void Cancel()
        {
            txtId.Text = "";
            txtName.Text = "";
            LoadStatus(false, false, false, false, false);
            LoadData();
        }

        public override void Save()
        {
            if (CheckValid())
            {
                try
                {
                    if (sModel.Trim().ToUpper().Equals(ADD_MODEL))
                    {
                        AddCodeType();
                    }
                    else if (sModel.Trim().ToUpper().Equals(UPDATE_MODEL))
                    {
                        UpdateCodeType();
                    }
                    MessageBox.Show("Cập nhật thành công.", "Aquarius", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    LoadData();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message + Environment.NewLine + "Không thể cập nhật.", "Aquarius Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
        #endregion

        #region Method
        private void BuildTree()
        {
            TreeNode mainNode = new TreeNode();
            mainNode.Name = "mainNode";
            mainNode.Text = "Danh mục";
            treeView1.Nodes.Add(mainNode);

            TreeNode gender = mainNode.Nodes.Add("Giới tính");
            TreeNode nation = mainNode.Nodes.Add("Quốc tịch");
            TreeNode edu = mainNode.Nodes.Add("Học vấn");
            TreeNode bank = mainNode.Nodes.Add("Ngân hàng");
            TreeNode koc = mainNode.Nodes.Add("Loại khách hàng");
            //TreeNode serv = mainNode.Nodes.Add("Dịch vụ");
            TreeNode trans = mainNode.Nodes.Add("Hãng vận chuyển");
            
            treeView1.ImageIndex = 0;
            treeView1.Nodes[0].Expand();
        }

        private void LoadData()
        {
            lvwItem.Items.Clear();
            ArrayList arrCodeType = boCodeType.GetListCodeTypeByGroup(sGroup);
            if (arrCodeType != null)
            {
                if (arrCodeType.Count > 0)
                {
                    ListViewItem item;
                    foreach (DataObjects.CodeType oCodeType in arrCodeType)
                    {
                        item = new ListViewItem();
                        item.Text = oCodeType.CodeTypeId;
                        item.SubItems.Add(oCodeType.CodeTypeName);
                        lvwItem.Items.Add(item);
                    }
                    if (lvwItem.Items.Count > 0)
                        lvwItem.Items[0].Selected = true;
                    else
                        LoadStatus(sGroup.Length > 0 ? true : false, false, false, false, false);
                }
                else
                    LoadStatus(sGroup.Length > 0 ? true : false, false, false, false, false);
            }
        }

        private void LoadStatusOnce()
        {
            exeCtrl.btnFunction.Enabled = false;
            exeCtrl.btnView.Enabled = false;
            exeCtrl.btnFind.Enabled = false;
            exeCtrl.btnExport.Enabled = false;
            exeCtrl.btnSendmail.Enabled = false;
            exeCtrl.btnPrint.Enabled = false;

            exeCtrl.btnFunction.BackColor = Color.FromArgb(171, 171, 171);
            exeCtrl.btnView.ButtonColor = Color.FromArgb(171, 171, 171);
            exeCtrl.btnFind.ButtonColor = Color.FromArgb(171, 171, 171);
            exeCtrl.btnExport.ButtonColor = Color.FromArgb(171, 171, 171);
            exeCtrl.btnSendmail.ButtonColor = Color.FromArgb(171, 171, 171);
            exeCtrl.btnPrint.ButtonColor = Color.FromArgb(171, 171, 171);

            exeCtrl.btnFunction.ForeColor = Color.FromArgb(241, 244, 251);
            exeCtrl.btnView.ForeColor = Color.FromArgb(241, 244, 251);
            exeCtrl.btnFind.ForeColor = Color.FromArgb(241, 244, 251);
            exeCtrl.btnExport.ForeColor = Color.FromArgb(241, 244, 251);
            exeCtrl.btnSendmail.ForeColor = Color.FromArgb(241, 244, 251);
            exeCtrl.btnPrint.ForeColor = Color.FromArgb(241, 244, 251);
        }

        private void LoadStatus(bool blAdd, bool blEdit, bool blDelete, bool blCancel, bool blSave)
        {
            exeCtrl.btnAdd.Enabled = blAdd & bAdd;
            exeCtrl.btnEdit.Enabled = blEdit & bEdit;
            exeCtrl.btnDelete.Enabled = blDelete & bDelete;
            exeCtrl.btnCancel.Enabled = blCancel & bCancel;
            exeCtrl.btnSave.Enabled = blSave & bSave;

            exeCtrl.btnAdd.ButtonColor = exeCtrl.btnAdd.Enabled ? Color.FromArgb(0, 163, 211) : Color.FromArgb(171, 171, 171);
            exeCtrl.btnEdit.ButtonColor = exeCtrl.btnEdit.Enabled ? Color.FromArgb(0, 163, 211) : Color.FromArgb(171, 171, 171);
            exeCtrl.btnDelete.ButtonColor = exeCtrl.btnDelete.Enabled ? Color.FromArgb(0, 163, 211) : Color.FromArgb(171, 171, 171);
            exeCtrl.btnCancel.ButtonColor = exeCtrl.btnCancel.Enabled ? Color.FromArgb(0, 163, 211) : Color.FromArgb(171, 171, 171);
            exeCtrl.btnSave.ButtonColor = exeCtrl.btnSave.Enabled ? Color.FromArgb(0, 163, 211) : Color.FromArgb(171, 171, 171);

            exeCtrl.btnAdd.ForeColor = exeCtrl.btnAdd.Enabled ? Color.FromArgb(0, 0, 0) : Color.FromArgb(241, 244, 251);
            exeCtrl.btnEdit.ForeColor = exeCtrl.btnEdit.Enabled ? Color.FromArgb(0, 0, 0) : Color.FromArgb(241, 244, 251);
            exeCtrl.btnDelete.ForeColor = exeCtrl.btnDelete.Enabled ? Color.FromArgb(0, 0, 0) : Color.FromArgb(241, 244, 251);
            exeCtrl.btnCancel.ForeColor = exeCtrl.btnCancel.Enabled ? Color.FromArgb(0, 0, 0) : Color.FromArgb(241, 244, 251);
            exeCtrl.btnSave.ForeColor = exeCtrl.btnSave.Enabled ? Color.FromArgb(0, 0, 0) : Color.FromArgb(241, 244, 251);
        }

        private void IsEdit()
        {
            if (sModel == ADD_MODEL | sModel == UPDATE_MODEL)
            {
                //txtId.ReadOnly = false;
                txtName.ReadOnly = false;
                lvwItem.Enabled = false;
                treeView1.Enabled = false;
            }
            else
            {
                //txtId.ReadOnly = true;
                txtName.ReadOnly = true;
                lvwItem.Enabled = true;
                treeView1.Enabled = true;
            }
        }

        private bool CheckValid()
        {
            if (txtName.Text.Trim().Length == 0)
            {
                MessageBox.Show("Vui lòng nhập tên.", "Aquarius", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtName.Focus();
                return false;
            }
            return true;
        }

        private void AddCodeType()
        {
            //Add new 
            DataObjects.CodeType oCodeType = new DataObjects.CodeType();
            oCodeType.CodeTypeId = txtId.Text;
            oCodeType.CodeTypeName = txtName.Text;
            oCodeType.GroupId = sGroup;

            //Insert
            boCodeType.Insert(oCodeType);
        }

        private void UpdateCodeType()
        {
            if (sGroup == "")
            {
                return;
            }

            //update
            DataObjects.CodeType oCodeType = new DataObjects.CodeType();
            oCodeType.CodeTypeId = txtId.Text;
            oCodeType.CodeTypeName = txtName.Text;
            oCodeType.GroupId = sGroup;

            boCodeType.Update(oCodeType);
        }
        #endregion

        
  
    }
}
